import 'package:flutter/material.dart';
import 'home_screen.dart';
import 'history_screen.dart';
import 'add_expense_screen.dart';

class MainScaffold extends StatefulWidget {
  final int initialTab;
  const MainScaffold({Key? key, this.initialTab = 0}) : super(key: key);

  @override
  State<MainScaffold> createState() => _MainScaffoldState();
}

class _MainScaffoldState extends State<MainScaffold> {
  late int _currentIndex;

  @override
  void initState() {
    super.initState();
    _currentIndex = widget.initialTab;
  }

  @override
  Widget build(BuildContext context) {
    final pages = [
      const HomeScreen(),
      Container(), // 占位，记账tab不直接显示内容
      const HistoryScreen(),
    ];
    return Scaffold(
      extendBody: true,
      body: pages[_currentIndex],
      bottomNavigationBar: Container(
        decoration: BoxDecoration(
          color: Colors.transparent,
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.08),
              blurRadius: 12,
              offset: const Offset(0, -2),
            ),
          ],
        ),
        child: ClipRRect(
          borderRadius: const BorderRadius.only(
            topLeft: Radius.circular(24),
            topRight: Radius.circular(24),
          ),
          child: BottomNavigationBar(
            backgroundColor: Colors.white,
            elevation: 0,
            type: BottomNavigationBarType.fixed,
            selectedItemColor: Colors.blueAccent,
            unselectedItemColor: Colors.grey,
            selectedLabelStyle: const TextStyle(fontWeight: FontWeight.bold),
            unselectedLabelStyle:
                const TextStyle(fontWeight: FontWeight.normal),
            iconSize: 28,
            currentIndex: _currentIndex,
            onTap: (i) async {
              if (i == 1) {
                final result = await Navigator.push(
                  context,
                  MaterialPageRoute(builder: (_) => const AddExpenseScreen()),
                );
                if (result == true) {
                  setState(() {
                    _currentIndex = 0;
                  });
                  ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(
                      content: const Text('记账成功！', textAlign: TextAlign.center),
                      backgroundColor: Colors.green,
                      behavior: SnackBarBehavior.floating,
                      margin: const EdgeInsets.fromLTRB(40, 0, 40, 80),
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.all(Radius.circular(12))),
                      duration: Duration(seconds: 2),
                    ),
                  );
                }
              } else {
                setState(() {
                  _currentIndex = i;
                });
              }
            },
            items: const [
              BottomNavigationBarItem(
                  icon: Icon(Icons.home_rounded), label: '首页'),
              BottomNavigationBarItem(
                  icon: Icon(Icons.add_circle_outline), label: '记账'),
              BottomNavigationBarItem(
                  icon: Icon(Icons.history_edu_rounded), label: '历史'),
            ],
          ),
        ),
      ),
    );
  }
}
